package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import model.ByGroupModel;
import model.ByLastAddedModel;
import model.Contact;
import model.ContactListModel;
import view.ContactWindow;
import view.MainWindow;

/**
 * Call the model to order the users by the date of their add (from the last added to the first added in the 
 * contact application).
 * @author Marie Morin
 *
 */
public class LastAddedController implements ActionListener {
	
	
	private MainWindow w;
	private String text;	
	private ByLastAddedModel m;
	private ArrayList<Contact> contactsList = new ArrayList<Contact>();

	/**
	 * Constructor
	 * @param w, main window
	 */
	public LastAddedController(MainWindow w){
		
		this.w = w;		

		// Create a list with all the contacts of the file 
		ContactListModel c = new ContactListModel();	
		contactsList = c.getContactsList();	
	
	}


	public ArrayList<Contact> getContactsList() {
		return contactsList;
	}

	public void setContactsList(ArrayList<Contact> contactsList) {
		this.contactsList = contactsList;
	}

	/**
	 * This function is called when the user click on the LastAdded button.
	 * It creates a new list with the contacts ordered by the date of their add, from the last one added to the first one added. 
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

		w.setStaticSearch("lastAdded");
		
		m = new ByLastAddedModel(this);
		
		
		if (contactsList.size()==0)
		{
			w.setLayoutNull();
		}
		else
		{
			w.setLayoutByGroup(this.contactsList);
		}

		
		
	}

}
